//
//  DreamSchoolViewController.m
//  HotWord
//
//  Created by Rainbow on 8/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DreamSchoolViewController.h"
#import "ServiceHelper.h"
#import "UserManager.h"
#import "SchoolCell.h"

@interface DreamSchoolViewController ()
@end

@implementation DreamSchoolViewController
@synthesize schoolTableView;
@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[ServiceHelper sharedService] requestServerForMethod:HotWordRequestTypeListSchool WithParameters:nil AndCallBackScope:self AndSuccessSel:@"listSchoolSuccess:" AndFailedSel:@"listSchoolFailed:"];
    if ([[UserManager sharedManager].schools count] <= 0) {
        [[HTActivityIndicator currentIndicator] displayActivity:@"Get Schools..."];
    }
}

- (void)listSchoolSuccess:(NSObject *)result
{
    if (![result isKindOfClass:[NSDictionary class]]) {
        return;
    }
    NSDictionary *dic = (NSDictionary *)result;
    NSString *code = [dic objectForKey:@"code"];
    if ([@"0" isEqualToString:code]) {
        NSArray *schoolDics = [dic objectForKey:@"data"];
        if ([schoolDics isKindOfClass:[NSArray class]] && [schoolDics count] > 0) {
            NSMutableArray *schools = [NSMutableArray array];
            for (NSDictionary *schoolDic in schoolDics) {
                School *school = [[[School alloc] init] autorelease];
                school.schoolId = [UtilHelper getStringValue:[schoolDic objectForKey:@"schoolId"]];
                school.schoolEn = [UtilHelper getStringValue:[schoolDic objectForKey:@"schoolEn"]];
                school.schoolCn = [UtilHelper getStringValue:[schoolDic objectForKey:@"schoolCn"]];
                school.schoolState = [UtilHelper getStringValue:[schoolDic objectForKey:@"schoolState"]];
                school.order = [UtilHelper getStringValue:[schoolDic objectForKey:@"order"]];
                [schools addObject:school];
            }
            [[UserManager sharedManager] setAllSchools:schools];
            [self.schoolTableView reloadData];
        }
    }
}

- (void)listSchoolFailed:(NSObject *)result
{
    
}

- (void)viewDidUnload
{
    [self setSchoolTableView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[HTActivityIndicator currentIndicator] hide];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"SchoolCell";
    
    SchoolCell *cell = (SchoolCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SchoolCell" owner:self options:nil] objectAtIndex:0];
    }
    
    School *school = [[UserManager sharedManager].schools objectAtIndex:indexPath.row];
    [cell setSchool:school];
    return cell;

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[UserManager sharedManager].schools count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    School *school = [[UserManager sharedManager].schools objectAtIndex:indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(DreamSchoolViewControllerDidSelected:)]) {
        [self.delegate DreamSchoolViewControllerDidSelected:school];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)tapOnBackBtn:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc {
    [schoolTableView release];
    [super dealloc];
}
@end
